var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

function getAllWare1(){ //获取所有发货仓
    var sel = document.getElementById("dcName"); //发货仓添加
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/abaseservice/area/getAllWarehouse',
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        success: function(obj){
            for(var i=0;i<obj.data.length;i++){
                var option = new Option(obj.data[i].warehouse_name,obj.data[i].warehouse_code);
                sel.options.add(option)
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });

}

var updateData = "";

function getAllDispatch(){
    var warehouseName = $('#dcName option:selected').val();
    var status = $('#status option:selected').val();
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/alogistics/findDispatchModeByPage.jhtml',
        cache: false, //禁用缓存
        type: "POST",
        dataType:'json',
        data: {
            pageNum: 1,
            pageSize: 20,
            warehouseCode: warehouseName,
            status: status
        },
        success: function(result){
            if(result.errorCode=="0"){
                updateData = result;
                goodsList(result);
                $('.M-box3').pagination({
                    pageCount: result.data.totalPage, //总页数
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        $.ajax({
                            type: "POST",
                            url:'https://'+ window.location.host +'/api/admin/alogistics/findDispatchModeByPage.jhtml',
                            cache: false,  //禁用缓存
                            dataType: "json",
                            data: {
                                pageNum: api.getCurrent(),
                                pageSize: 20,
                                warehouseCode: warehouseName,
                                status: status
                            },
                            success: function (result) {
                                if(result.errorCode=="0"){
                                    updateData = result;
                                    goodsList(result);
                                }else {
                                    layer.msg(result.errorMessage, {icon: 5,time:1000});
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.log(XMLHttpRequest);
                                //alert(XMLHttpRequest);
                            }
                        });
                    }
                });
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            //alert(XMLHttpRequest);
        }
    });
}




function goodsList(result) {
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if(result.data != null){
        $.each(result.data.result, function(i, field) {
            var rowData = new Array();
            rowData.push(field.warehouseName);
            rowData.push(field.transportType);
            rowData.push(field.transportModeType);
            rowData.push(field.edgeLimit);
            rowData.push(field.weightLimit);
            rowData.push(field.volumeLimit);
            rowData.push(field.transportStatus);
            dataSource.push(rowData);
            rowData.push(field.id);
            rowData.push(field.maximumEdgeLimit);
            rowData.push(field.maxWeightLimit);
            rowData.push(field.minWeightLimit);
            rowData.push(field.volumeWeight);
        });
    }

    rangeDataTable(dataSource);
    function rangeDataTable(dataSource) {
        var dttable = $('#wayTable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#wayTable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row, meta) {
                        if(row[1] == 10){
                            return '自有物流';
                        }else if (row[1] == 20) {
                            return '第三方物流';
                        }
                    },
                    "targets": 1
                },

                {
                    "render": function(data, type, row, meta) {
                        if(row[2] == 10){
                            return '快递';
                        }else if (row[2] == 20) {
                            return '快运';
                        }else if (row[2] == 30) {
                            return '专车包车';
                        }else if (row[2] == 40) {
                            return '鑫方盛物流';
                        }

                    },
                    "targets": 2
                },
                {
                    "render": function(data, type, row, meta) {
                        if(row[3] == 10){
                            return "小于等于"+row[8]+"m";
                        }else if (row[3] == 20) {
                            return '无限制';
                        }

                    },
                    "targets": 3
                },

                {
                    "render": function(data, type, row, meta) {
                        if(row[4] == 10){
                            return "≤"+row[9]+"kg";
                        }else if (row[4] == 20) {
                            return '无限制';
                        }

                    },
                    "targets": 4
                },

                {
                    "render": function(data, type, row, meta) {
                        if(row[5] == 10){
                            return "(长cm*宽cm*高cm)/"+row[11];
                        }else if (row[5] == 20) {
                            return '无限制';
                        }

                    },
                    "targets": 5
                },

                {
                    "render": function(data, type, row, meta) {
                        if(row[6] == 10){
                            return '有效';
                        }else{
                            return '无效';
                        }
                    },
                    "targets": 6
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '<a class="powerButton 139" onclick="relation_update(\'修改运送方式\','+row[7]+',)"><span id="areaList" class="list">修改</span>';
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '';
                        return val;
                    },
                    "targets": 8
                },
            ]
        });
        //权限控制函数
        power()
    };
}

function relation_add(title,url){
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


function relation_update(title,wayId){
    var index = layer.open({
        type: 2,
        title: title,
        content: 'transportWay-update.html?wayId='+wayId
    });
    layer.full(index);
}


function serchWay(){
    getAllDispatch();
}